<?php


namespace app\admin\controller;


use app\admin\model\AuthGroup;
use app\BaseController;
use app\Request;
use fast\Tree;
use think\facade\View;

class User extends BaseController
{
    /**
     * @var \app\admin\model\Admin
     */
    protected $childrenGroupIds = [];
    protected $childrenAdminIds = [];
    protected $group = [];

    public function initialize()
    {
        parent::initialize();

        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);

        $groupList = collection(AuthGroup::where('id', 'in', $this->childrenGroupIds)->select())->toArray();

        Tree::instance()->init($groupList);
        $groupdata = [];
        if ($this->auth->isSuperAdmin()) {
            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0));
            foreach ($result as $k => $v) {
                $groupdata[$v['id']] = $v['name'];
            }
        } else {
            $result = [];
            $groups = $this->auth->getGroups();
            foreach ($groups as $m => $n) {
                $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']));
                $temp = [];
                $temp[$n['id']] = $n['name'];
                foreach ($childlist as $k => $v) {
                    $temp[$v['id']] = $v['name'];
                }
                $result = $temp;
            }
            $groupdata = $result;
        }
        $this->group = $groupdata;
        View::assign('group', $this->group);
    }

    //更新资料
    public function info(Request $request)
    {
        $group_id = "";
        foreach ($this->auth->getGroups() as $key => $v) {
            $group_id = $v['group_id'];
        }
        $admin = \app\admin\model\Admin::find($this->auth->id);
        $admin['group_id'] = $group_id;
        if ($request->isAjax()) {
            $data = $request->param();
            $data['id'] = $this->auth->id;
            $res = \app\admin\model\Admin::update($data);
            if ($res) {
                return json(['code' => 200, 'url' => '', 'msg' => '更新成功']);
            }
        }
        View::assign('admin', $admin);
        return View::fetch();
    }


    public function password(Request $request)
    {

        if ($request->isAjax()) {
            $data = $request->param();
            $admin = \app\admin\model\Admin::find($this->auth->id);
            $oldPassword = md5(sha1($data['oldPassword']));
            if ($oldPassword != $admin['password']) {
                return json(['code' => 400, 'url' => '', 'msg' => '当前密码输入错误']);
            }
            if ($data['password'] != $data['repassword']) {
                return json(['code' => 400, 'url' => '', 'msg' => '两次密码输入不一致']);
            }

            $data['id'] = $this->auth->id;
            $data['password'] = md5(sha1($data['password']));
            $res = \app\admin\model\Admin::update($data);
            if ($res) {
                return json(['code' => 200, 'url' => '', 'msg' => '更新成功']);
            }


        }

        return View::fetch();
    }
}